using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using Renderer.DataStructures;
using Renderer.Utils;

namespace Renderer
{
    class SimpleSceneLoader : ISceneLoader
    {
        public Scene LoadScene(string fileName)
        {
            Scene result = new Scene();
            try
            {                
                StreamReader reader = new StreamReader(fileName);
                while (!reader.EndOfStream)
                {                    
                    string[] s = reader.ReadLine().Split(' ');
                    switch(s[0])
                    {
                        case "//" : 
                            break;
                            
                        case "camera":                            
                            result.CameraPosition = Vector3D.FromString(s[1], ';');
                            result.CameraDirection = Vector3D.FromString(s[2], ';');
                            break;
                            
                        case "triangle":
                            Triangle triangle = new Triangle(
                                Vector3D.FromString(s[1], ';'),
                                Vector3D.FromString(s[2], ';'),
                                Vector3D.FromString(s[3], ';')
                                );
                            triangle.Color = Vector3D.FromString(s[4], ';').ToColor();

                            triangle.IsLight = (s[5] == "light");

                            if (triangle.IsLight)
                            {
                                result.Triangles.Add(triangle);
                            }
                            else
                            {
                                result.LightTriangles.Add(triangle);
                            }
                            break;
                        default:
                            ExceptionHandler.Handle("Unknown token in scene file");
                            break;
                    }
                }
            }
            catch(Exception e)
            {
                ExceptionHandler.Handle("Problems with reading scene from file", e);
            }
            return result;
        }
    }
}
